package com.zzyl.nursing.service.impl;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.PageUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.AlertDataPageQuery;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
//        // 查询当前用户id
//        Long userId = SecurityUtils.getUserId();
////        根据用户id查询角色id
//        Long roleId = sysUserMapper.getRoleIdByUserId(userId);
//        // 根据角色id决定查询范围
//        // 1.超级管理员:查询所有报警信息
//        if (roleId == 1L) {
//            PageUtils.startPage();
//            return alertDataMapper.selectAlertDataList(alertData);
//        } else if (roleId == 100L){
//            // 2.护理员：查询自己负责老人的报警信息.
//            // 护理员id ----负责老人id列表 ---床位id----设备id---老人报警信息
//            // 得到iotId
//            String iotId = deviceDataMapper.getIotIdByUserId(userId);
//            alertData.setIotId(iotId);
//            // 通过iotid 查询报警信息
//            PageUtils.startPage();
//            return alertDataMapper.selectAlertDataListByIotId(alertData);
//        }
//        else if (roleId == 101L) {
//            // 3.维修员：查询自己负责的设备报警信息
//            // 维修员id ----设备id---roomid-----设备报警信息
//            alertData.setUserId(userId);
//            PageUtils.startPage();
//            return alertDataMapper.selectAlertDataListByUserId(alertData);
//        }
//        else {
//            return Collections.emptyList();
//        }


        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<AlertData> alertDataList = this.lambdaQuery()
                .eq(AlertData::getUserId, userId)
                .eq(StrUtil.isNotBlank(alertData.getDeviceName()), AlertData::getDeviceName, alertData.getDeviceName())
                .eq(alertData.getId() != null, AlertData::getId, alertData.getId())
                .between(alertData.getStartTime() != null && alertData.getEndTime() != null, AlertData::getCreateTime, alertData.getStartTime(), alertData.getEndTime())
                .eq(alertData.getStatus() != null, AlertData::getStatus, alertData.getStatus())
                .orderByDesc(AlertData::getCreateTime)
                .list();
        if(CollUtil.isEmpty(alertDataList)){
            return Collections.emptyList();
        }
        return alertDataList;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }


    /**
     * 处理报警数据
     *
     * @param dto
     * @return
     */
    @Override
    public void handleAlertData(AlertDataDto dto) {
        AlertData alertData = this.lambdaQuery().eq(AlertData::getId, dto.getId()).one();
        BeanUtil.toBean(alertData, AlertDataDto.class );
        this.lambdaUpdate()
                .set(AlertData::getStatus, 1)
                .set(AlertData::getProcessorId, SecurityUtils.getUserId())
                .set(AlertData::getProcessorName, SecurityUtils.getLoginUser().getUsername())
                .set(AlertData::getProcessingTime, new Date())
                .set(AlertData::getProcessingResult, dto.getProcessingResult())
                .eq(AlertData::getIotId, alertData.getIotId())
                .eq(AlertData::getCreateTime, alertData.getCreateTime()).update();
//        String processorName = SecurityUtils.getLoginUser().getUsername();
//        dto.setProcessorName(processorName);
//        alertDataMapper.handleAlertData(dto);
    }
}
